package com.mi.rule.security.resource;

import com.mi.rule.util.ContextKit;
import com.mi.rule.util.EnvKit;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * 资源处理器（用于扫描注解）
 */
public class ResourceHandler {
    /**
     * 全部资源
     */
    private static ArrayList<ResEntity> resList = null;


    public static ArrayList<ResEntity> getResList() {
        return ResourceHandler.resList;
    }

    /**
     * 项目启动时
     * 调用此方法扫描系统中全部资源
     * ResourceHandler.getResList() 读取全部资源
     */
    public static synchronized void scan() {
        if (resList == null) {
            resList = new ArrayList<>();

            //扫描所有具备Res资源注解的Class
            for (Object bean : ContextKit.getContext().getBeansWithAnnotation(Res.class).values()) {

                //检查资源完整性
                Class<?> aClass = bean.getClass();
                Res resController = AnnotationUtils.findAnnotation(aClass, Res.class);
                RestController restController = AnnotationUtils.findAnnotation(aClass, RestController.class);
                RequestMapping requestMapping = AnnotationUtils.findAnnotation(aClass, RequestMapping.class);
                if (resController == null || restController == null || requestMapping == null) continue;

                //遍历所有控制器
                for (String controllerValue : requestMapping.value()) {
                    //资源路径
                    String controllerPath = EnvKit.contextPath() + controllerValue;

                    ResEntity moduleEntity = new ResEntity();
                    moduleEntity.setUrl(controllerPath);
                    moduleEntity.setPUrl(null);
                    moduleEntity.setName(resController.value());
                    moduleEntity.setDesc(resController.desc());
                    resList.add(moduleEntity);

                    //处理方法
                    Method[] declaredMethods = aClass.getDeclaredMethods();
                    for (Method method : declaredMethods) {
                        RequestMapping methodMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
                        ResApi resApi = AnnotationUtils.findAnnotation(method, ResApi.class);
                        if (methodMapping == null || resApi == null) continue;

                        //遍历所有方法
                        for (String methodValue : methodMapping.value()) {
                            ResEntity resEntity = new ResEntity();
                            resEntity.setUrl(controllerPath + methodValue);
                            resEntity.setPUrl(controllerPath);
                            resEntity.setName(resApi.value());
                            resEntity.setDesc(resApi.desc());
                            resList.add(resEntity);
                        }
                    }

                }
            }
        }

    }


}
